package com.fili.gui;

import com.fili.params.Param;
import com.fili.params.ParametrizationEngine;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.util.ArrayList;

/**
 * Date: Nov 16, 2005
 * Time: 6:05:18 PM
 *
 * @author Claudiu-Florin FILIMON for Borux Consulting S.R.L.
 */
public class ComponentContentSaver
{
    private ParametrizationEngine pEngine;
    private String prefix;
    private ArrayList<CCSComponent> items;
/*-------------------------------------------------------------------------------------
    Getters and setters
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
	Constructors
 --------------------------------------------------------------------------------------*/

    public ComponentContentSaver(ParametrizationEngine hParams, String prefix)
    {
        this.pEngine = hParams;
        this.prefix = prefix;
        items = new ArrayList<CCSComponent>();
    }
/*-------------------------------------------------------------------------------------
    Methods
 --------------------------------------------------------------------------------------*/

    public void addComponent(Component comp, String paramName, Object defaultData)
    {
        paramName = prefix + "." + paramName;
        Param p = pEngine.getParam(paramName);

        if (p != null)
        {
            defaultData = p.getCurrentValue();
        }

        CCSComponent ccsc = new CCSComponent(comp, paramName, defaultData);

        loadData(ccsc);

        items.add(ccsc);
    }

    public void saveAll()
    {
        for (CCSComponent ccsc : items)
        {
            Param p = pEngine.addParam(new Param(ccsc.paramName, ccsc.defaultData));

            if (ccsc.comp instanceof JCheckBox)
                p.setCurrentValue(((JCheckBox) ccsc.comp).isSelected());
            if (ccsc.comp instanceof JRadioButton)
                p.setCurrentValue(((JRadioButton) ccsc.comp).isSelected());
            if (ccsc.comp instanceof JTextComponent)
                p.setCurrentValue(((JTextComponent) ccsc.comp).getText());
            if (ccsc.comp instanceof JComboBox)
            {
                if (((JComboBox) ccsc.comp).isEditable())
                    p.setCurrentValue(((JComboBox) ccsc.comp).getEditor().getItem());
                else
                    p.setCurrentValue(((JComboBox) ccsc.comp).getSelectedIndex());
            }
        }
    }

    public void undoChanges()
    {
        for (CCSComponent ccsc : items)
            loadData(ccsc);
    }

    private void loadData(CCSComponent ccsc)
    {
        if (ccsc.comp instanceof JCheckBox)
            ((JCheckBox) ccsc.comp).setSelected((Boolean) (ccsc.defaultData));
        if (ccsc.comp instanceof JRadioButton)
            ((JRadioButton) ccsc.comp).setSelected((Boolean) (ccsc.defaultData));
        if (ccsc.comp instanceof JTextComponent)
            ((JTextComponent) ccsc.comp).setText((String) (ccsc.defaultData));
        if (ccsc.comp instanceof JComboBox)
        {
            JComboBox cb = ((JComboBox) ccsc.comp);
            if (ccsc.defaultData instanceof String)
                cb.getEditor().setItem((String) ccsc.defaultData);
            else
            {
                int v = (Integer) (ccsc.defaultData);
                if (v >= cb.getItemCount())
                    v = cb.getItemCount() - 1;
                cb.setSelectedIndex(v);
            }
        }
    }
/*-------------------------------------------------------------------------------------
    Overrides
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
    Implementations
 --------------------------------------------------------------------------------------*/


    class CCSComponent
    {
        Component comp;
        String paramName;
        Object defaultData;

        public CCSComponent(Component comp, String paramName, Object defaultData)
        {
            this.comp = comp;
            this.paramName = paramName;
            this.defaultData = defaultData;
        }
    }
}
